package com.example.baiduai.dao.impl;

import com.example.baiduai.dao.NlpDao;
import com.example.baiduai.entity.Emotion;
import com.example.baiduai.entity.Items;
import com.example.baiduai.tools.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author wangyizhu
 * @version "1.8.0_131"
 * @email 2641956765@qq.com
 * @desc:
 * @Time 2022/05/27 9:04
 */
public class NlpDaoImpl implements NlpDao {
    QueryRunner queryRunner = new QueryRunner(DbUtils.getDataSource());

    @Override
    public Emotion SelectEmotion(String text) {
        // 待执行的sql语句
        String sql = "SELECT * FROM emotion where `text`=?  ";
        try {
            Emotion list = queryRunner.query(sql,new BeanHandler<>(Emotion.class),text);
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int insertEmotion(String text,List<Items> list) {
        String sql = "insert into emotion (text,emotion_dir,confidence,positive_prob,negative_prob) values(?,?,?,?,?)";
        //置信度
        double Confidence = list.get(0).getConfidence();
        //积极概
        double positive_prob = list.get(0).getPositive_prob();
        //消极
        double negative_prob = list.get(0).getNegative_prob();

        int sentiment=list.get(0).getSentiment();
        String s="";
        if (sentiment==2){
            s="正向";
        }else if(sentiment==1){
            s="中性";
        }else if(sentiment==0){
            s="负向";
        }

        try {
            int update = queryRunner.update(sql, new Object[]{text,s,Confidence,positive_prob,negative_prob});
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
